package org.yscz.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.yscz.entity.RoomBean;
import org.yscz.media.MediaService;
import org.yscz.service.impl.EvaluationServiceImpl.NotifyAlgBusiness;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author achen
 * @create 2021-08-23 9:32
 */
@Component
@WebListener
@Slf4j
public class ProjectDestroyConfig implements ServletContextListener {

    @Autowired
    private MediaService mediaService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("项目销毁");
        RoomBean analyzingRoomBean = NotifyAlgBusiness.getAnalyzingRoomBean();
        if(analyzingRoomBean != null) {
            NotifyAlgBusiness.notifyAlgStopAnalyze(analyzingRoomBean.getUuid());
            mediaService.delSteam(analyzingRoomBean.getUuid());
            mediaService.stopUsbStream();
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("项目启动");
    }
}
